package com.foreknow.elm.service.impl;

import com.foreknow.elm.dao.DeliveryaddressDao;
import com.foreknow.elm.dao.impl.DeliveryaddressDaoImpl;
import com.foreknow.elm.po.Deliveryaddress;
import com.foreknow.elm.service.DeliveryaddressService;
import com.foreknow.elm.util.DBUtil;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class DeliveryaddressServiceImpl implements DeliveryaddressService {
    @Override
    public List<Deliveryaddress> listDeliveryAddressByUserId(String userId) {
        List<Deliveryaddress> list = new ArrayList<>();
        DeliveryaddressDao deliveryaddressDao = new DeliveryaddressDaoImpl();
        try {
            list = deliveryaddressDao.listDeliveryAddressByUserId(userId);
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            DBUtil.close();
        }
        return list;
    }

    @Override
    public Deliveryaddress getDeliveryAddressById(Integer daId) {
        Deliveryaddress deliveryaddress = new Deliveryaddress();
        DeliveryaddressDao deliveryaddressDao = new DeliveryaddressDaoImpl();
        try {
            deliveryaddress = deliveryaddressDao.getDeliveryAddressById(daId);
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            DBUtil.close();
        }
        return deliveryaddress;
    }

    @Override
    public int saveDeliveryAddress(String contactName, Integer contactSex, String contactTel, String address, String userId) {
        int i = 0;
        DeliveryaddressDao deliveryaddressDao = new DeliveryaddressDaoImpl();
        try {
            i = deliveryaddressDao.saveDeliveryAddress(contactName,contactSex,contactTel,address,userId);
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            DBUtil.close();
        }
        return i;
    }

    @Override
    public int updateDeliveryAddress(Integer daId, String contactName, Integer contactSex, String contactTel, String address, String userId) {
        int i = 0;
        DeliveryaddressDao deliveryaddressDao = new DeliveryaddressDaoImpl();
        try {
            i = deliveryaddressDao.updateDeliveryAddress(daId,contactName,contactSex,contactTel,address,userId);
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            DBUtil.close();
        }
        return i;
    }

    @Override
    public int removeDeliveryAddress(Integer daId) {
        int i = 0;
        DeliveryaddressDao deliveryaddressDao = new DeliveryaddressDaoImpl();
        try {
            i = deliveryaddressDao.removeDeliveryAddress(daId);
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            DBUtil.close();
        }
        return i;
    }
}
